import { StoreOptions } from "vuex";
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService } from "../../generated";

/**
 * 存储用户信息
 */
export default {
  namespace: true,
  state: () => ({
    userInfo: {
      userName: "未登录",
      userRole: ACCESS_ENUM.NOT_LOGIN,
    },
  }),
  mutations: {
    updateUser(state, payload) {
      state.userInfo = payload;
    },
  },
  actions: {
    async getLoginUser({ commit }) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet();
      if (res.code === 0) {
        commit("updateUser", {
          userName: res.data?.userName ?? "用户",
          userRole: res.data?.userRole,
        });
      } else {
        commit("updateUser", {
          userName: "未登录",
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
  },
} as StoreOptions<any>;
